package com.gxa.autoparts.admin.timerTask;


import com.gxa.autoparts.admin.service.SystemMessageService;
import com.gxa.autoparts.core.common.ExceptionCode;
import com.gxa.autoparts.core.common.JsonResult;
import com.gxa.autoparts.core.exception.BusinessException;
import com.gxa.autoparts.core.pojo.SystemMessage;
import com.gxa.autoparts.core.util.DateUtil;
import com.gxa.autoparts.core.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.sql.Timestamp;
import java.util.*;

@Component
public class TimerTask implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        time();
    }

    public void time() throws BusinessException {
        /*Timer timer = new Timer();
        RestTemplate restTemplate = new RestTemplate();
        MultiValueMap<String,Object> multiValueMap = new LinkedMultiValueMap();
        multiValueMap.add("time","1002222");
        multiValueMap.add("msg",message().get("msg"));
        Date date = new Date();
        long time = date.getTime();
        timer.schedule(new java.util.TimerTask() {
            @Override
            public void run() {
                try {
                    if (time==Long.parseLong(message().get("time1"))){
                        System.out.println("---------------------执行线程方法");
                    }else {
                        throw new BusinessException(ExceptionCode.WRONG_NO_MESSAGE);
                    }
                    JsonResult jsonResult = restTemplate.postForObject("http://localhost:8080/admin/systemMessage/send", multiValueMap, JsonResult.class);
                } catch (Exception b){
                    try {
                        throw new BusinessException(ExceptionCode.WRONG_NO_MESSAGE);
                    } catch (BusinessException e) {
                        e.printStackTrace();
                    }
                }
            }
        },1000,1000);*/
    }

    /*@Autowired
    private SystemMessageService systemMessageService;
    public Map<String,String> message() throws BusinessException {
        List<SystemMessage> recentMessage = systemMessageService.findRecentMessage();
        if (recentMessage.size()==0){
            throw new BusinessException(ExceptionCode.WRONG_NO_MESSAGE);
        }
        SystemMessage systemMessage = recentMessage.get(0);
        System.out.println("______________________________\n"+systemMessage);
        Map<String,String> map = new HashMap<>();
        Map<String,Object> map2 = new HashMap<>();
        map2.put("msg",systemMessage);
        map.put("msg", JsonUtil.ObjectToString(map2));
        map.put("time", DateUtil.getSecond(systemMessage.getSendTime()));
        map.put("time1", String.valueOf(systemMessage.getSendTime().getTime()));
        System.out.println("-------------------time:"+map.get("time"));
        return map;
    }*/


}
